package com.tianqu.tms.www.cardect.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.www.cardect.viewhelper.CarWDIpadTopListViewHelper;

/**
 * 待检测IPAD首页tab页Controller
 * 
 * @author tianshifu
 * 
 */
public class CarWDIpadTopDispatchController extends TmsController {

	private Logger logger = Logger
			.getLogger(CarWDIpadTopDispatchController.class);// log
	private static final String DISPATCH_LIST = "allocateDispatchList";

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			// 从tiscarinfo表中获取待检测来源和条数
			TmsDetectInfo car = TmsDetectInfo.getInstance();

			List<TmsDetectInfo> tisList = car
					.getCarInfoSourseAndCountsByStatus(TmsDetectInfo.WAIT_DETECT_TIS);// tis待检测信息

			List<TmsDetectInfo> ipadList = car
					.getCarInfoSourseAndCountsByStatus(TmsDetectInfo.WAIT_DETECT_IPAD);// ipad待检测信息
			// 将list set 到viewelp中
			CarWDIpadTopListViewHelper viewhelp = new CarWDIpadTopListViewHelper();

			List<CarWDIpadTopListViewHelper> viewList = new ArrayList<CarWDIpadTopListViewHelper>();

			// 服务器端
			int tisCounts = 0;
			if (tisList.size() > 0) {
				for (int i = 0; i < tisList.size(); i++) {
					TmsDetectInfo entity = (TmsDetectInfo) tisList.get(i);
					tisCounts += entity.getCounts();
					// viewhelp.setCounts();
				}
				if (tisCounts > 0) {
					viewhelp = new CarWDIpadTopListViewHelper();
					viewhelp.setCounts(tisCounts);
					viewhelp.setSourse("服务器端");
					viewList.add(viewhelp);
				}
			}

			// 客户端
			int ipadCounts = 0;
			if (ipadList.size() > 0) {
				for (int i = 0; i < ipadList.size(); i++) {
					TmsDetectInfo entity = (TmsDetectInfo) ipadList.get(i);
					ipadCounts += entity.getCounts();
				}
				if (ipadCounts > 0) {
					viewhelp = new CarWDIpadTopListViewHelper();
					viewhelp.setCounts(ipadCounts);
					viewhelp.setSourse("客户端");
					viewList.add(viewhelp);
				}
			}

			viewhelp.setDispatchList(viewList);

			uc.setAttribute(DISPATCH_LIST, viewList);// 用于下个页面读取
			uc.removeAttribute(TmsConstants.VIEWHELP);
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);// 将来源和条数打到前台
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#doAction was completed.");

	}

}
